# a function that computes the average (arithmetic mean) and standard deviation, element-wise, for all matrices within a structure array
# version: Nov 2018
# ENSURE that Octave can find the function, use the "addpath" command: addpath ("/[insert the path to your Octave folder]/Octave")

# function arguments:
# name - a structure array of matrices of the same size

function [mean, std] = TCD_struct (name)
# get the number of fields in the structure
nfs = numfields (name);
# get the size of matrices within the structure, handling the first field
fsize = size(name.(cell2mat(fieldnames(name)(1))));
# compute the average
struct_sum = zeros(fsize);
for [val, key] = name
	struct_sum = struct_sum + val;
endfor
mean = struct_sum/nfs;
# compute the standard deviation
struct_sumsq = zeros(fsize);
for [val, key] = name
	struct_sumsq = struct_sumsq + (val - mean).^2;
endfor
std = (struct_sumsq./(nfs-1)).^0.5;
endfunction
